<%@ page import="com.hxzy.bean.User" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.ArrayList" %><%--
  Created by IntelliJ IDEA.
  User: Administrator
  Date: 9/22
  Time: 13:55
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" isELIgnored="false" %>

<%--通过taglib指令导入标签库--%>
<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
    <title>Title</title>
</head>
<body>
    <%
        /*向四个作用域中保存了键为user的值*/
        request.setAttribute("user","request_user");  //user只会在一起请求中有效
        session.setAttribute("user", "session_user"); //user只会在本次会话中有效
        application.setAttribute("user", "application_user");   //user在全局中有效
        pageContext.setAttribute("user", "pageContext_user");   //user只会在当前页有效

        User user = new User(1, "大黄", "dahuang", "123456", 1);
        //将对象添加到作用域
        pageContext.setAttribute("dog",user);
    %>

    <h2>获取作用域中的数据</h2>
    <ul>
        <li>当前页：<%=pageContext.getAttribute("user")%></li>
        <li>request作用域：<%=request.getAttribute("user")%></li>
        <li>session作用域：<%=session.getAttribute("user")%></li>
        <li>application作用域：<%=application.getAttribute("user")%></li>
    </ul>

    <h2>在jsp2.0以后，内置了el表达式</h2>
    <ul>
        <li>page作用域：${pageScope.user}</li>  <!--从page作用域中获取键为user的值-->
        <li>request作用域：${requestScope.user}</li>
        <li>session作用域：${sessionScope.user}</li>
        <li>application作用域：${applicationScope.user}</li>
        <!--
            如果不指定作用域，则按照：  pageScope -> requestScope -> sessionScope -> applicationScope -> 空
        -->
        <li>自动查找作用域：${user}</li>
        <li>dog：${dog}</li>
        <li>id：${dog.id}</li>
        <li>nickName：${dog.nickName}</li>
    </ul>

    <!--通过标准标签库中的forEach遍历作用域中的集合-->
    <%
        User user1 = new User(1, "大黄1", "dahuang1", "123456", 1);
        User user2 = new User(2, "大黄2", "dahuang2", "123456", 1);
        User user3 = new User(3, "大黄3", "dahuang3", "123456", 1);
        User user4 = new User(4, "大黄4", "dahuang4", "123456", 1);
        User user5 = new User(5, "大黄5", "dahuang5", "123456", 1);

        List<User> list = new ArrayList<>();
        list.add(user1);
        list.add(user2);
        list.add(user3);
        list.add(user4);
        list.add(user5);

        //将list集合添加到当前页的作用域中
        pageContext.setAttribute("list", list);
    %>
    <c:forEach items="${list}" var="user"> <!--用这个标签遍历list集合-->
        <!--在jstl标签库内编写html代码-->
        <ul>
            <li>id:${user.id}</li>
            <li>nickName:${user.nickName}</li>
            <li>account:${user.account}</li>
        </ul>
    </c:forEach>
</body>
</html>
